package vn.keho.module.dompeto.fragment;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bingkaimedia.merchantbeta.R;
import com.google.zxing.BarcodeFormat;
import com.jwetherell.quick_response_code.data.Contents;
import com.jwetherell.quick_response_code.qrcode.QRCodeEncoder;

import org.json.JSONObject;

import java.util.Iterator;

import vn.keho.define.convert.JsonUtil;
import vn.keho.module.dompeto.model.QrCodeModel;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 * Use the {link vn.keho.module.dompeto.fragment.LeftFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class QrCodeFragment extends Fragment {
    public static final int QRCODE_FRAGMENT_ID = 2;


    private TextView txtServiceTitle;
    private ImageView imgQrCode;
    private TextView txtQrCodeDetail;


    private QrCodeModel qrCodeModel;

    // TODO: Rename and change types and number of parameters
    public static QrCodeFragment newInstance(QrCodeModel qrCodeModel) {
        QrCodeFragment fragment = new QrCodeFragment();
        fragment.qrCodeModel = qrCodeModel;
        return fragment;
    }

    public QrCodeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_qrcode, container, false);

        txtServiceTitle = (TextView) view.findViewById(R.id.txtServiceTitle);
        txtServiceTitle.setText(qrCodeModel.getChanneltitle() + " - " + qrCodeModel.getServicetitle());

        imgQrCode = (ImageView) view.findViewById(R.id.imgQrCode);

        txtQrCodeDetail = (TextView) view.findViewById(R.id.txtQrCodeDetail);
        generateQrCode();
        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    
    private void generateQrCode() {
        //Encode with a QR Code image
        try {
            String data = JsonUtil.buildJsonString(qrCodeModel.getQrcodeinformation());
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(data,
                    null,
                    Contents.Type.TEXT,
                    BarcodeFormat.QR_CODE.toString(),
                    400);
            Bitmap bitmap = qrCodeEncoder.encodeAsBitmap();

            imgQrCode.setImageBitmap(bitmap);

            JSONObject jsonObject = new JSONObject(data);
            Iterator<?> keys = jsonObject.keys();

            String dataFormat = "";
            while( keys.hasNext() ){
                String key = (String)keys.next();
                String value = jsonObject.getString(key);

                dataFormat += key + ": " + value + "\n";
            }
            txtQrCodeDetail.setText(dataFormat);
        } catch(Exception e) {
            e.printStackTrace();
        }

    }
}
